import axios from 'axios'
import {
    ElMessage,
    ElLoading
} from 'element-plus'
import router from '../router'
import {
    refrestTokenKC
} from '@/utils/setting'
// 获取本地时区
let ZoneId = Intl.DateTimeFormat().resolvedOptions().timeZone;
let loading = null
let loadTime = null
let loadingCount = 0
// import store from '@/store'
const instance = axios.create({
    // 根路径
    // baseURL: 'http://172.16.1.148:40080',
    baseURL: 'http://www.xpertbay.com',
    timeout: 50000
})
// console.log(store, '00000000');
// 添加请求拦截器
instance.interceptors.request.use(
    function (config) {
        // console.log(config, 'config555555555555555555555555555555');
        // if (loading) {
        //     loading.close()
        // }
        if (loadingCount === 0) {
            loading = ElLoading.service({
                lock: true,
                text: 'Loading...',
                background: 'rgba(225, 225, 225, 0.8)'
            })
        }
        loadingCount++
        // 在发送请求之前做些什么
        let token = 'Bearer ' + localStorage.getItem('kcToken') || ''
        config.headers['Authorization-Kc'] = token
        // 卖家买家
        let ind = localStorage.getItem('ind')
        if (ind == 3 || ind == 6) {
            // 表示买家
            config.headers.identity = 1
        } else if (ind == 2 || ind == 5) {
            // 卖家
            config.headers.identity = 2
        }
        config.headers['Zone-Id'] = ZoneId
        return config
    },

    function (error) {
        //201提示
        loadingCount--
        if (loadingCount === 0) {
            setTimeout(() => {
                loading.close()

            }, 500);
        }
        // 对请求错误做些什么
        return Promise.reject(error)
    }
)
// 添加响应拦截器
instance.interceptors.response.use(
    async function (response) {
            loadingCount--
            if (loadingCount === 0) {
                setTimeout(() => {
                    loading.close()

                }, 500);
            }
            // if (!loadTime) {
            // }
            if (response.data.code == 431) {
                // 表示已经注册但是没有验证邮箱
                ElMessage('Please bind email')
                setTimeout(() => {
                    // router.push("/Login/VerificationCode");
                    router.push({
                        path: "/Login/VerificationCode",
                        query: {
                            act: 'kc'
                        }
                    });
                }, 1000)
            }
            if (response.data.code != 200 && response.data.code != 401 && response.data.code != 431 && response.data.code != 4502 && response.data.code != 4501) {
                ElMessage(response.data.msg)
            }
            if (response.data.code == 4501) {
                // 买家账户信息未完善
                ElMessage('Please complete your personal information')
                setTimeout(() => {
                    // 去用户详情
                    router.push("/BuyKnowUseMyProfile");
                }, 1000);
            }
            if (response.data.code == 401) {
                // ElMessage('Please log in first')
                // 如果没有token那就去登录
                if (!localStorage.getItem('kcToken')) {
                    ElMessage('Please log in first')
                    setTimeout(() => {
                        router.replace('/Login?act=kc')
                    }, 800);
                    return
                }
                // 表示有token但是token过期了
                await refrestTokenKC()
                setTimeout(() => {
                    router.replace('/KnowClub')
                }, 800);
            }
            if (response.data.code == 4502) {
                // // 卖家账户信息未完善
                ElMessage('Please complete your personal information')
                setTimeout(() => {
                    // 去用户详情
                    router.push("/BuyKnowUseMyProfile");
                }, 1000);
            }


            return response.data
        },
        function (error) {
            loadingCount--
            if (loadingCount === 0) {
                setTimeout(() => {
                    loading.close()

                }, 500);
            }
            // console.log(error, '2222222');
            // showToast
            // 对响应错误做点什么
            return Promise.reject(error)
        }
)
export default instance